<template>
	<theme-box title="收到消息查询">
		<form class="form-inline" @submit.prevent v-el:searchform>
			<div class="form-group">
				<label>商户编号：</label>
				<input type="text" class="form-control" v-el:searchid>
			</div>
			<v-button theme="warning" class="pull-right" @click="reset">重置</v-button>
			<v-button theme="primary" class="pull-right" @click="search">查询</v-button>
		</form>
		<table class="table fixed table-bordered table-striped" v-el:table>
		</table>
	</theme-box>
</template>

<style scoped>
	.form-inline .btn+.btn {
		margin-right: 10px;
	}
	
	.form-inline .form-group+.form-group {
		margin-left: 20px;
	}
</style>

<script>
	import {
		deepClone
	}
	from 'lib/clone.js';

	let datatable;
	export default {
		data: () => ({
			dataSet: {
				data: [],
				recordsTotal: 0,
				recordsFiltered: 0
			},
			columnsSet: []
		}),
    events: {
      'table-reload': function () {
      	datatable.api().ajax.reload();
			}
    }, 		
		methods: {
			  reset() {
					this.$els.searchform.reset();
					datatable.api().ajax.reload();
				},
				search() {
					datatable.api().ajax.reload();
				},
				setConfig() {
					//..加载配置
					let self = this;
						this.columnsSet = [
						{
								title: "商户编号",
								data: "id.mchtId",
								className: "first-td"
						},
						{
								title: "公众微信号",
								data: "fromUsrName"
						},	
						{
								title: "发送账号",
								data: "toUsrName"
						},		
						{
								title: "消息类型",
								data: "msgType",
							  render: function(data) {
									if(data == 'event') {
										return '事件消息'
									} else {
										return '普通消息'
									}
								}								
						},					
						{
							title: "内容",
							data: "content"
						}							
						//picUrl														
					];
				},
				loadData(data = {}) {
					//..加载数据
					let self = this;
					return new Promise(function (resolve, reject) {
						self.$search({
							url: '/api/v1' + projectName_Url + '/wechat/reqMsg/query.html',
							data
						}).then( response => {	
							if (response) {
								let content = response.content;
								self.dataSet.data = content.reqMsgList;
								self.dataSet.recordsTotal = content.total;
								self.dataSet.recordsFiltered = content.total;
								return resolve();
							}
						}, response => {
							  return reject();
						});

					})
				},
				getCollection() {
					let collection = {};
					let self = this;
					this.$post({
						url: "/api/v1" + projectName_Url + "/sys/role/query.html"
					}).then(function (response) {
						collection.role = response.info;
					});
					return collection;
				}
		},
		created() {
			this.setConfig();
		},
		ready() {
			let self = this;

			datatable = $(this.$els.table).dataTable({
				"columns": self.columnsSet,
				"paging": true,
				"pagingType": "full_numbers",
				"lengthChange": false,
				"searching": false,
				"ordering": false,
				"info": true,
				"autoWidth": false,
				"serverSide": true,
				"processing": true,
				"ajax": function (data, callback, settings) {

					let result = {};
					let _data = {};

					_data.aoData = JSON.stringify([{
						"name": "iDisplayStart",
						"value": 1 + data.start / data.length
									}, {
						"name": "iDisplayLength",
						"value": data.length
									}]);

					$(self.$els.searchform).find("input").each(function () {
						$(this).val($.trim($(this).val()));
					});
					
					_data.mchtIdFlag = self.$els.searchid.value;

					self.loadData(_data).then(function () {
						result = self.dataSet;
						if (!result.data) {
							result.data = [];
						}
						callback(result);
					}, function () {
						result.data = [];
						callback(result);
					})

				},
				"fnDrawCallback": function( settings ) {
					let count = Math.ceil(settings._iRecordsTotal/settings._iDisplayLength);
					if(count<=1) {
						  $('.dataTables_paginate').hide();
					} else {
						  $('.dataTables_paginate').show();
					}
				}				

			});
		}
	}
</script>